package com.wptk.filemanager.ui.clean;

import android.content.Intent;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.Toolbar;

import com.airbnb.lottie.LottieAnimationView;
import com.wptk.filemanager.App;
import com.wptk.filemanager.R;
import com.wptk.filemanager.model.FileDataManager;
import com.wptk.filemanager.model.clean.ThridApp;
import com.wptk.filemanager.ui.base.InterruptActivity;
import com.wptk.filemanager.utils.ThreadManager;

import java.util.List;

public class BatteryScanActivity extends InterruptActivity {

    private static final int HANDLE_LOOP =  1;
    private static final int HANDLE_END =  2;
    private LottieAnimationView av;
    private Toolbar toolbar;
    private TextView scoreTv;
    private TextView perTv;
    private List<ThridApp> thridAppList;
    private ImageView appIconIv;
    private TextView appPackageTv;

    private Handler handler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(@NonNull Message msg) {

            if(isQuit) return;

            switch (msg.what){
                case HANDLE_LOOP:
                    ThridApp app = (ThridApp) msg.obj;
                    //msg.getData()

                    appIconIv.setImageDrawable(app.getAppIcon());
                    appPackageTv.setText(app.getPackageName());
                    break;
                case HANDLE_END:

                    Intent intent = new Intent(BatteryScanActivity.this, ScanResultActivity.class);
                    intent.putExtra(ScanResultActivity.TYPE, ScanResultActivity.TYPE_BATTERY);
                    intent.putExtra(ScanResultActivity.NUMBER, String.valueOf(thridAppList.size()));
                    App.thridAppList = thridAppList;//临时存储，页面传递数据使用
                    startActivity(intent);
                    finish();
                    break;
            }
        }
    };


    @Override
    protected int getLayoutId() {
        return R.layout.activity_scan_battery;
    }

    @Override
    protected void initView() {
        toolbar = findViewById(R.id.toolbar);
        scoreTv = findViewById(R.id.score_tv);
        perTv = findViewById(R.id.per_tv);
        appIconIv = findViewById(R.id.app_icon_iv);
        appPackageTv = findViewById(R.id.app_package_tv);

    }

    @Override
    protected void initEvent() {

        toolbar.setTitle(R.string.power_saving);
        thridAppList = FileDataManager.getInstance().getScanResultList();

        scoreTv.setText(String.valueOf(thridAppList.size()));
        perTv.setText("apps");


        showScanFile(thridAppList);
    }

    /**
     * 显示扫描包名
     */
    private void showScanFile(List<ThridApp> thridAppList){

        if(thridAppList == null || thridAppList.size()==0) return;

        ThreadManager.getInstance().execute(new Runnable() {
            @Override
            public void run() {

                int scanNum = 0;
                while(scanNum<30){

                    for(ThridApp thridApp : thridAppList){
                        if(scanNum>=30) break;

                        Message message = handler.obtainMessage();
                        message.what = HANDLE_LOOP;
                        message.obj = thridApp;
                        //Bundle bundle = new Bundle();
                        //bundle.putSerializable("app", thridApp);
                        //message.setData(bundle);
                        handler.sendMessage(message);
                        scanNum ++ ;
                        try {
                            Thread.sleep(100);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }

                //循环完成跳转
                handler.sendEmptyMessage(HANDLE_END);
            }
        });
    }
}
